package org.cuckoos.android.command;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.cuckoos.android.domain.Command;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.ui.AppActivity;

/**
 * 
 * @author bugu
 *
 */
public abstract class AbstractCommand {
	protected DatagramSocket ds;
//	protected AppActivity activity;

	public AbstractCommand(DatagramSocket ds){
		this.ds = ds;
	}

	public abstract void execute();
	
	public abstract void handle(Command ac);
	
	public abstract boolean isHandle(Command ac);
	
	protected void execute(Command command) {
		String ip = command.getUser().getIp()!=null ? command.getUser().getIp() : "255.255.255.255";
		
		try {
			byte[] b = command.toString().getBytes(Constant.DEFAULT_CHARSET);
			DatagramPacket dp = new DatagramPacket(b,b.length, InetAddress.getByName(ip),Constant.DEFAULT_PORT);
			ds.send(dp);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	
//	public AppActivity getActivity() {
//		return activity;
//	}
//
//	public void setActivity(AppActivity activity) {
//		this.activity = activity;
//	}
}
